﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// transforms  by replacing each pixel with the most common pixel value in its range  neighborhood.
    /// </summary>
    public class CommonestFilter : BaseImageFilter
    {
        protected int m_nSizeX = 3;
        protected int m_nSizeY = 3;

        /// <summary>
        /// 
        /// </summary>
        public CommonestFilter()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_commonestFilter"></param>
        internal CommonestFilter(CommonestFilter _commonestFilter)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public int SizeX
        {
            get { return m_nSizeX; }
            set { m_nSizeX = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public int SizeY
        {
            get { return m_nSizeY; }
            set { m_nSizeY = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;
            int x = 0;
            int y = 0;
            int nMin = 0;

            Color colorTemp = Color.Transparent;
            Color color = Color.Transparent;
            Color colorCommonest = Color.Transparent;

            int kx = 0;
            int ky = 0;

            int nX = 0;
            int nY = 0;

            int m_nSizeHalf = 6*2 / 2 + 1;
            for (y = m_nSizeHalf; y < nHeight - m_nSizeHalf-1; y++)
            {
                for (x = m_nSizeHalf; x < nWidth - m_nSizeHalf-1; x++)
                {
                    color = bitmapCloned.GetPixel(x, y);
                    nMin = int.MaxValue;
                    
                    for (ky = -m_nSizeHalf; ky < m_nSizeHalf; ky++)
                    {
                        for (kx = -m_nSizeHalf; kx < m_nSizeHalf; kx++)
                        {
                            nX = x + kx;
                            nY = y + ky;
                            if (nX == x && nY == y)
                            {
                                continue;
                            }
                            colorTemp = bitmapCloned.GetPixel(nX, nY);
                            int nValue = ColorFunctions.DistanceSquared(color, colorTemp);
                            if (nValue < nMin)
                            {
                                if (nValue == 0) break;
                                nMin = nValue;
                                colorCommonest = colorTemp;
                            }
                        }
                    }
                    _bitmap.SetPixel(x, y, colorCommonest);
                    if (colorCommonest != color)
                    {
                        int n1 = 0;
                    }

                }
            }
            return _bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new CommonestFilter(this);
        }
    }
}
